import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

/// toast 工具类
class XToast {
  // 初始化 Toast
  static TransitionBuilder initToast({TransitionBuilder? builder}) {
    return EasyLoading.init(builder: builder);
  }

  // 配置
  static void setToastConfig() {
    EasyLoading.instance
      ..displayDuration = const Duration(milliseconds: 2000)
      ..indicatorType = EasyLoadingIndicatorType.fadingCircle
      ..loadingStyle = EasyLoadingStyle.custom
      ..animationStyle = EasyLoadingAnimationStyle.opacity
      ..maskType = EasyLoadingMaskType.custom
      ..backgroundColor = const Color(0x99000000)
      ..maskColor = Colors.black54
      ..indicatorColor = Colors.white
      ..textColor = Colors.white
      ..textStyle = const TextStyle(
        color: Colors.white,
        fontSize: 15,
      )
      ..progressColor = Colors.white
      ..indicatorSize = 40
      ..radius = 12
      ..contentPadding = const EdgeInsets.all(20)
      ..userInteractions = false
      ..dismissOnTap = false;
  }

  // 显示加载提示
  static void showLoading({String? text}) {
    EasyLoading.show(
      status: text,
      indicator: const CupertinoActivityIndicator(
        color: Colors.white,
      ),
    );
  }

  // 显示文本提示
  static void showText(String text, {Duration? duration}) {
    EasyLoading.showToast(text, duration: duration);
  }

  // 加载成功
  static void showSuccess(String text, {Widget? successWidget, Duration? duration}) {
    if (successWidget != null) {
      EasyLoading.instance.successWidget = successWidget;
    }
    EasyLoading.showSuccess(text, duration: duration);
  }

  // 加载失败
  static void showError(String text, {Widget? errorWidget, Duration? duration}) {
    if (errorWidget != null) {
      EasyLoading.instance.errorWidget = errorWidget;
    }
    EasyLoading.showError(text, duration: duration);
  }

  // 警示
  static void showInfo(String text, {Widget? infoWidget, Duration? duration}) {
    if (infoWidget != null) {
      EasyLoading.instance.infoWidget = infoWidget;
    }
    EasyLoading.showInfo(text, duration: duration);
  }

  // 进度加载 '${(progress * 100).toStringAsFixed(0)}%'
  static void showProgress(double progress, {String? text}) {
    EasyLoading.showProgress(progress, status: text);
  }

  // 关闭 toast
  static void dismiss() {
    EasyLoading.dismiss();
  }
}
